<?php
function get_menu ()
{

	return array(
		'home' => array(
			'title' => 'Home'
		),
		'article_list' => array(
			'title' => 'Articles',
			'items' => array(
				'article_list' => array(
					'title' => 'List'
				),
				'article_create' => array(
					'title' => 'Create'
				)
			)
		),
		'channel_list' => array(
			'title' => 'Channels',
			'items' => array(
				'channel_list' => array(
					'title' => 'List'
				),
				'channel_create' => array(
					'title' => 'Create'
				)
			)
		),
		'block_list' => array(
			'title' => 'Blocks',
			'items' => array(
				'block_list' => array(
					'title' => 'List'
				),
				'block_create' => array(
					'title' => 'Create'
				)
			)
		),
		 'newsstream_list' => array(
			'title' => 'Newsfeed list'
		),
		'section_list' => array(
			'title' => 'Sections',
			'items' => array(
				'section_list' => array(
					'title' => 'List'
				),
				'section_create' => array(
					'title' => 'Create'
				)
			)
		)
	);
}

function get_content ()
{
	$content['article_list'] = array(
		'type' => 	'list',
		'title' => 'Articles',
		'query' => 	'SELECT * FROM om_articles',
		'edit' => array (
			'page' => 'article_create',
			'column' => 'id' 
		),
		'columns' => array(
			array('header' => 'ID', 'db_column' => 'id', 'width' => 30),
			array('header' => 'Header', 'db_column' => 'header', 'width' => 30),
		)
	);
	$content['article_create'] = array(
		'type' => 	'edit',
		'title' => 'Create article',
		'query' => 	'SELECT * FROM om_articles WHERE id = @id',
		'fields' => array(
			array('type' => 'text', 'title' => 'Header', 'db_column' => 'header', 'width' => 30),
			array('type' => 'text', 'title' => 'Teaser text', 'db_column' => 'teaser_text', 'width' => 30),
			array('type' => 'text', 'title' => 'Teaser link', 'db_column' => 'teaser_link', 'width' => 30),
			array('type' => 'formattext', 'title' => 'Body', 'db_column' => 'body', 'width' => 30),
		)
	);
	
	$content['block_list'] = array(
		'type' => 	'list',
		'query' => 	'SELECT * FROM om_blocks',
		'columns' => array(
			array('header' => 'ID', 'db_column' => 'id', 'width' => 30),
			array('header' => 'Name', 'db_column' => 'name', 'width' => 30),
		)
	);
	$content['newsstream_list'] = array(
		'type' => 	'list',
		'query' => 	'SELECT * FROM om_blocks WHERE type = "newsstream"',
		'edit' => array(
			'page' => 'newsstream_order',
			'column' => 'id'
		),
		'columns' => array(
			array('header' => 'ID', 'db_column' => 'id', 'width' => 30),
			array('header' => 'Name', 'db_column' => 'name', 'width' => 30),
		)
	);
	
	$content['newsstream_order'] = array(
		'type' => 	'edit',
		'title' => 'Order newsstream articles',
		'query' => 	'
		SELECT n.*, a.header
		FROM om_newsstream AS n
		LEFT OUTER JOIN om_articles AS a ON a.id = n.article_id 
		WHERE block = @id
		ORDER BY n.sortorder ASC
		',
		'update_order' => 'UPDATE om_newsstream SET sortorder = @order WHERE id = @id',
		'fields' => array(
			array('type' => 'sortlist', 'title' => 'Order'),
		)
	);
	
	
	$content['channel_list'] = array(
		'type' => 	'list',
		'query' => 	'SELECT * FROM om_channels',
		'edit' => array(
			'page' => 'block_order',
			'column' => 'id'
		),
		'columns' => array(
			array('header' => 'ID', 'db_column' => 'id', 'width' => 30),
			array('header' => 'Name', 'db_column' => 'name', 'width' => 30),
		)
	);
	$content['block_order'] = array(
		'type' => 	'edit',
		'title' => 'Order blocks',
		'update_order' => "UPDATE om_blocks SET sortorder = @order WHERE id = @id;",
		'query' => 	'SELECT id, name as header FROM om_blocks WHERE channel = @id ORDER BY sortorder ASC',
		'fields' => array(
			array('type' => 'sortlist', 'title' => 'Order'),
		)
	);
	$content['section_list'] = array(
		'type' => 	'edit',
		'title' => 'Order sections',
		'update_order' => "UPDATE om_sections SET sortorder = @order WHERE id = @id;",
		'query' => 	'SELECT id, name as header FROM om_sections ORDER BY sortorder ASC',
		'fields' => array(
			array('type' => 'sortlist', 'title' => 'Order'),
		)
	);
	return $content;
}

function process_list ($content, $results)
{
	$title = isset($content['title']) ? $content['title'] : '';
	$ret =  '
	';
	$ret .= '<table border="0" cellpadding="5" style="font-size:11px;">';
	$ret .= '<tr>';
	if (isset($content['edit']))
	{
		$ret .= '<td></td>';
	}
	foreach ($content['columns'] as $col)
	{
		
		$ret .= '<td>' . $col['header'] . '</td>';
	}
	$ret .= '</tr>';
	foreach ($results as $row)
	{
		$ret .= '<tr>';
		if (isset($content['edit']))
		{
			$ret .= '<td><a href="'.site_url('admin/page/'.$content['edit']['page'].'/'.$row->$content['edit']['column']).'"><img src="images/icons/edit.png" style="border:none;"/></td>';
		}
		foreach ($content['columns'] as $col)
		{
			$ret .= '<td>' . $row->$col['db_column'] . '</td>';
		}
		$ret .= '</tr>';
	}
	$ret .= '</table>
	';
	
	return $ret;
}
function process_edit($content, $results)
{
	$ret = '';
	/*
	 * set input default value
	 */
	
	$ret .= '<table>';
	foreach ($content['fields'] as $field)
	{
		//print_r($results);
		$value = '';
		if ($results != NULL && $field['type'] != 'sortlist')
		{
			$value = $results[0]->$field['db_column'];
		}
		
		if ($field['type'] == 'text')
		{
			$ret .= '<tr><td width="150">' . $field['title'] . '</td>';
			$ret .= '<td><input type="text" value="'.$value.'"></td>';
			$ret .= '</tr>';
		}
		if ($field['type'] == 'sortlist')
		{
			$ret .= '<div class="demo">
			';
			$ret .= '<ul id="sortable">';
			$i = 0;
			foreach ($results as $r)
			{
				
				$ret .= '<li id="box|'.$content['page'].'|'.$r->id.'" class="ui-state-default">' . $r->header . '</li>';
				$i++;
			}
			$ret .= '</ul>';

			$ret .= '</div>';
		}
		else if ($field['type'] == 'formattext')
		{
			$ret .= '<tr><td width="150">' . $field['title'] . '</td>';
			$ret .= '
			<td valign="top">
				<form>  
				<textarea name="content" cols="50" rows="15" > 
				'.$value.'
				</textarea>
				</form>
			</td>
			';
			$ret .= '</tr>';
		}
	}
	$ret .= '</table>';
	return $ret;
}
?>